
class User:
    # id,name,age,sex,addr,school,cls
    def __init__(self, **kwargs):
        self.id = kwargs.get("id")
        self.name = kwargs.get("name")
        self.age = kwargs.get("age")
        self.sex = kwargs.get("sex")
        self.addr = kwargs.get("addr")
        self.school = kwargs.get("school")
        self.cls = kwargs.get("cls")

class SingletonClass:
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            print(" ====new =========")
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self,**kwargs):

        print(" ====init =========")
        if not hasattr(self, '_initialized'):  # 避免多次初始化
            self._initialized = True
            # 初始化代码
            self.user_dict = {}

    def add_user(self,id,user):
        self.user_dict[id]=user


    def get_user(self,id):
        return self.user_dict.get(id)


if __name__ == '__main__':
    user1 = User(id=1, name="张三", age=18, sex="男", addr="北京", school="清华", cls="数学")
    user2 = User(id=2, name="李四", age=20, sex="女", addr="上海", school="北大", cls="英语")
    sc = SingletonClass()
    sc.add_user(1,user1)
    sc.add_user(2,user2)
    sc2 = SingletonClass()
    print("==========取内存数据信息==============")
    print(sc2.get_user(1).__dict__)
    print(sc2.get_user(2).__dict__)
    print(sc2.get_user(3))
    print(sc2.get_user(4))
    print(id(sc),id(sc2))



